<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button class="set">设置</button>
    <button class="get">获取</button>
</body>
<script>
    // cookie的特性:
    // 1. cookie是以键值对形式存在的字符串(key=val)
    // 2. cookie的存取都依赖于document.cookie
    // 3. cookie可以设置存储路径
    // 4. cookie可以设置过期时间 (默认:浏览器关闭过期)



    // cookie的创建:
    // 1. cookie是以键值对形式存在的字符串(key=val)
    // 2. cookie的存取都依赖于document.cookie
    // 3. cookie存储的时候是一条一条存储的, 取的时候将所有的cookie整合到一整个字符串中 -> 用"; "分隔

    var setBtn = document.getElementsByClassName("set")[0];
    var getBtn = document.getElementsByClassName("get")[0];


    setBtn.onclick = function () {

        var date = new Date();
        date.setDate(date.getDate() + 7);  //7天后过期

        // console.log(date.toString());
        // console.log(date.toUTCString());

        // // date.toString()     将日期对象转化为字符串(中国标准时间)
        // // date.toUTCString()  将日期对象转化为字符串(世界时 =>中国标准时间-8 )


        // // document.cookie = "user=a123123; expires=" + date.toUTCString() + "; path=/day32";
        // // document.cookie = "pwd=123123; expires=" + date.toUTCString() + "; path=/day32";
        // // document.cookie = "phone=17386141517; expires=" + date.toUTCString() + "; path=/day32";
        // // document.cookie = "email=123123@163.com; expires=" + date.toUTCString() + "; path=/day32";


        // document.cookie = "user=a123123; expires=" + date.toUTCString() + "; path=/";
        // document.cookie = "pwd=123123; expires=" + date.toUTCString() + "; path=/";
        // document.cookie = "phone=17386141517; expires=" + date.toUTCString() + "; path=/";
        // document.cookie = "email=123123@163.com; expires=" + date.toUTCString() + "; path=/";


        setCookie("user", "a123123", 7 * 24 * 60 * 60);
        setCookie("pwd", "123123");
        setCookie("phone", "17386141517", 1 * 24 * 60 * 60);
        setCookie("email", "a123123@163.com", 31 * 24 * 60 * 60);
    }



    getBtn.onclick = function () {
        console.log(document.cookie);

        // var cookie = document.cookie;
        // if (cookie) {
        //     var list = cookie.split("; ")
        //     console.log(list);
        //     for (var i = 0; i < list.length; i++) {
        //         var item = list[i];
        //         var k = item.split("=")[0];
        //         var v = item.split("=")[1];
        //         console.log(k, v);
        //     }
        // }

        console.log(getCookie("user"));
        console.log(getCookie("pwd"));
        console.log(getCookie("phone"));
        console.log(getCookie("email"));
        console.log(getCookie("abc"));

    }



    function setCookie(key, val, expires = null, path = "/") {
        if (expires == null || expires == undefined) {  // 不设置过期时间
            document.cookie = key + "=" + val + "; path=" + path;
        } else {
            var date = new Date();
            date.setSeconds(date.getSeconds() + expires);
            document.cookie = key + "=" + val + "; expires=" + date.toUTCString() + "; path=" + path;
        }
    }

    function getCookie(key) {
        var cookie = document.cookie;
        if (cookie) {
            var list = cookie.split("; ")
            // console.log(list);
            for (var i = 0; i < list.length; i++) {
                var item = list[i];
                var k = item.split("=")[0];
                var v = item.split("=")[1];
                if (k === key) {
                    return v;
                }
            }
        }
        return "";
    }


</script>

</html>